package com.phs.main.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phs.main.common.constant.AjaxResult;
import com.phs.main.common.documents.PDFUtil;
import com.phs.main.web.po.Announcement;
import com.phs.main.web.service.IAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Trent
 * @since 2020-07-07
 */
@Api
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
	@Lazy
	@Autowired
	IAnnouncementService announcementService;

	@ApiOperation(value = "添加公告")
	@PostMapping("/addAnnouncement")
	public AjaxResult addAnnouncement(@RequestBody Announcement announcement) {
		try {
			announcementService.save(announcement);
			return AjaxResult.success("添加成功");
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

	@ApiOperation(value = "删除", notes = "根据ID删除公告")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "公告ID", dataType = "Long", required = true),
	})
	@DeleteMapping("deleteAnnouncementById/{id}")
	public AjaxResult deleteAnnouncementById(@PathVariable long id) {
		try {
			if (announcementService.removeById(id)) {
				return AjaxResult.success("已删除");
			} else {
				return AjaxResult.error("未删除");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("更新公告")
	@PutMapping("/updateAnnouncement")
	public AjaxResult updateAnnouncement(@RequestBody Announcement announcement) {
		try {
			if (announcementService.updateById(announcement)) {
				return AjaxResult.success("更改成功");
			} else {
				return AjaxResult.error("更改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("查询所有公告")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "页码", dataType = "Long"),
			@ApiImplicitParam(name = "size", value = "单页条数", dataType = "Long")
	})
	@GetMapping("/findAll/{current}/{size}")
	public AjaxResult findAll(@PathVariable Long current, @PathVariable(required = false) Long size) {
		if (current == null) {
			current = 1L;
		}
		if (size == null) {
			size = 10L;
		}
		Page<Announcement> page = new Page<>(current - 1, size);

		return AjaxResult.success(announcementService.page(page));
	}

	@ApiOperation("按照id查询公告")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", dataType = "Long")
	})
	@GetMapping("/findById/{id}")
	public AjaxResult findById(@PathVariable Long id) {
		return AjaxResult.success(announcementService.getById(id));
	}

	@ApiOperation("按照标题查询公告")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "title", value = "标题", dataType = "String")
	})
	@GetMapping("/findByTitle/{title}")
	public AjaxResult findByTitle(@PathVariable String title) {
		QueryWrapper<Announcement> announcementQueryWrapper = new QueryWrapper<>();
		announcementQueryWrapper.like("announcement_name", title);

		return AjaxResult.success(announcementService.list(announcementQueryWrapper));
	}

	@ApiOperation("按照日期查询公告")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "date", value = "时间", dataType = "Date"),
	})
	@GetMapping("/findByDate/{date}")
	public AjaxResult findByTitle(@PathVariable Date date) {
		QueryWrapper<Announcement> announcementQueryWrapper = new QueryWrapper<>();
		announcementQueryWrapper.eq("announcement_date", date);

		return AjaxResult.success(announcementService.list(announcementQueryWrapper));
	}

	@ApiOperation("下载公告pdf")
	@GetMapping("/announcementPDF")
	public void getAnnouncementPDF(HttpServletResponse response) throws IOException {
		PDFUtil.getPDF("announcementRecords_" + LocalDateTime.now(), announcementService.list(), response);
	}
}
